from main import db
from sqlalchemy.exc import InvalidRequestError
from main.utils import JSONHelper
from datetime import datetime
from sqlalchemy import or_, and_, func

def insert(object):
    try:
        if type(object) == list:
            for i in object:
                i.create_time = datetime.now()
                db.session.add(i)
        else:
            object.create_time = datetime.now()
            db.session.add(object)
        db.session.commit()
        return True
    # except InvalidRequestError as e:
    #     print(e)
    #     db.session.rollback()
    except Exception as e:
        print(e)
        return False


def update(object):
    try:
        # object.create_time = datetime.now()
        db.session.commit()
        return True
    except InvalidRequestError:
        db.session.rollback()
    except Exception as e:
        print(e)
        return False
